
package com.controller;

import com.Tool.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;


@RestController
@RequestMapping(value = "/park/interface")
public class CarApiController {
    static HCNetSDK hCNetSDK=HCNetSDK.INSTANCE;
    @GetMapping(value = "/openDoor")
    public boolean openDoor(){
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong IUserID;
        String ip="192.168.3.188";
        short port=8000;
        String userName="admin";
        String password="qwe123456789";
        IUserID=hCNetSDK.NET_DVR_Login_V30(ip,port,userName,password,m_strDeviceInfo);
        //设置门状态计划
        HCNetSDK.NET_DVR_DOOR_STATUS_PLAN status_plan=new HCNetSDK.NET_DVR_DOOR_STATUS_PLAN();

        status_plan.dwSize=status_plan.size();
        status_plan.dwTemplateNum=1;
        status_plan.write();
        Pointer lpInbuffer = status_plan.getPointer();
        boolean lc1=hCNetSDK.NET_DVR_SetDVRConfig(IUserID,HCNetSDK.NET_DVR_SET_DOOR_STATUS_PLAN,new NativeLong(1),lpInbuffer,status_plan.size());
        status_plan.read();
        System.out.println("aaaaaaaaaaaaaaaaaa"+lc1);
        //return lc1;


        //设置门状态计划模板1
        HCNetSDK.NET_DVR_PLAN_TEMPLATE plan_template=new HCNetSDK.NET_DVR_PLAN_TEMPLATE();
        plan_template.dwSize=plan_template.size();
        plan_template.byEnable=(byte)1;
        String bytem="aaaaaaaaaaaaaaa";
        plan_template.bytemplateName=new byte[HCNetSDK.TEMPLATE_NAME_LEN];
        for (int i = 0; i < bytem.length(); i++) {
            plan_template.bytemplateName[i] = (byte) bytem.charAt(i);
        }
        plan_template.dwWeekPlanNo=1;
        int m=0;
        plan_template.dwHolidayGroupNo=new int[HCNetSDK.MAX_HOLIDAY_GROUP_NUM];
        for(int i=0;i<16;i++) {
            plan_template.dwHolidayGroupNo[i] = m;
        }
        plan_template.write();
        Pointer lpbuffer2=plan_template.getPointer();
        boolean lc2=hCNetSDK.NET_DVR_SetDVRConfig(IUserID,HCNetSDK.NET_DVR_SET_DOOR_STATUS_PLAN_TEMPLATE,new NativeLong(1),lpbuffer2,plan_template.size());
        plan_template.read();
        System.out.println("bbbbbbbbbbb"+lc2);
        //return lc2;


        //设置门状态周计划
        HCNetSDK.NET_DVR_WEEK_PLAN_CFG plan_cfg=new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();
        plan_cfg.dwSize=plan_cfg.size();
        plan_cfg.byEnable=1;
        Pointer lpbuffer3=plan_cfg.getPointer();
        plan_cfg.write();
        plan_cfg.read();
        //HCNetSDK.NET_DVR_SINGLE_PLAN_SEGMENT_WEEK[]  plan_segment_weeks=new HCNetSDK.NET_DVR_SINGLE_PLAN_SEGMENT_WEEK[7];
        for(int i=0;i<7;i++) {
            for(int j=0;j<8;j++) {
                plan_cfg.struPlanCfg[i].struPlanCfgDay[j].byEnable=0;
                plan_cfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byHour =  0;
                plan_cfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byMinute = 0;
                plan_cfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.bySecond =  0;

                plan_cfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour =  23;
                plan_cfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour =  59;
                plan_cfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour =  59;
            }
        }
        for(int i=0;i<7;i++) {
            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].byEnable = 1;
            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].byDoorStatus = 3;
            //plan_segment[i].byVerifyMode=(byte)3;
            //plan_segment[i][0].struTimeSegment.struBeginTime=new HCNetSDK.NET_DVR_SIMPLE_DAYTIME();
            //plan_segment[i][0].struTimeSegment.struEndTime=new HCNetSDK.NET_DVR_SIMPLE_DAYTIME();
            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byHour = 0;
            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byMinute = 0;
            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.bySecond = 0;

            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byHour = 23;
            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byHour = 59;
            plan_cfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byHour = 59;
        }
        plan_cfg.write();

        boolean lc3=hCNetSDK.NET_DVR_SetDVRConfig(IUserID,HCNetSDK.NET_DVR_SET_WEEK_PLAN_CFG,new NativeLong(1),lpbuffer3,plan_cfg.size());

        if(!lc3) {
            int a = hCNetSDK.NET_DVR_GetLastError();
            System.out.println(a);
        }

      return  lc3;

       /* Long index=-1l;
        int status=1;
        Boolean kaimen=hCNetSDK.NET_DVR_ControlGateway(IUserID,index,status);
        return kaimen;*/

    }
    @GetMapping(value = "/setDoor")
    public void jBtnDoorCfg() throws UnsupportedEncodingException {
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong lUserID;
        String ip="192.168.3.188";
        short port=8000;
        String userName="admin";
        String password="qwe123456789";
        lUserID=hCNetSDK.NET_DVR_Login_V30(ip,port,userName,password,m_strDeviceInfo);
        int iErr = 0;

        HCNetSDK.NET_DVR_DOOR_CFG struDoorCfg = new HCNetSDK.NET_DVR_DOOR_CFG();
        struDoorCfg.write();

        IntByReference iBytesReturned = new IntByReference(0);
        if (false == hCNetSDK.NET_DVR_GetDVRConfig(lUserID, HCNetSDK.NET_DVR_GET_DOOR_CFG, new NativeLong(1), struDoorCfg.getPointer(), struDoorCfg.size(), iBytesReturned))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println( "NET_DVR_GET_DOOR_CFG失败，错误号：" + iErr);
            return;
        }
        struDoorCfg.read();
        System.out.println("NET_DVR_GET_DOOR_CFG成功！门1名称为：" + new String(struDoorCfg.byDoorName,"GBK"));

        byte[] strDoorName = "门1_lc3".getBytes("GBK");
        for (int i = 0; i < HCNetSDK.DOOR_NAME_LEN; i++)
        {
            struDoorCfg.byDoorName[i] = 0;
        }
        for (int i = 0; i <  strDoorName.length; i++)
        {
            struDoorCfg.byDoorName[i] = strDoorName[i];
        }

        struDoorCfg.write();

        if (false == hCNetSDK.NET_DVR_SetDVRConfig(lUserID, HCNetSDK.NET_DVR_SET_DOOR_CFG, new NativeLong(1), struDoorCfg.getPointer(), struDoorCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println("NET_DVR_SET_DOOR_CFG失败，错误号：" + iErr);
            return;
        }
        System.out.println( "NET_DVR_SET_DOOR_CFG成功！");
        hCNetSDK.NET_DVR_Logout(lUserID);
        hCNetSDK.NET_DVR_Cleanup();
        return;
    }
    @GetMapping(value = "/setTime")
    public void jBtnPlanCfg(){
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong lUserID;
        String ip="192.168.3.188";
        short port=8000;
        String userName="admin";
        String password="qwe123456789";
        lUserID=hCNetSDK.NET_DVR_Login_V30(ip,port,userName,password,m_strDeviceInfo);
        int iErr = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_COND struWeekPlanCond = new HCNetSDK.NET_DVR_WEEK_PLAN_COND();
        struWeekPlanCond.dwSize = struWeekPlanCond.size();
        struWeekPlanCond.dwWeekPlanNumber  = 1;
        struWeekPlanCond.wLocalControllerID = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_CFG struWeekPlanCfg = new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();

        IntByReference pInt = new IntByReference(0);
        Pointer lpStatusList = pInt.getPointer();

        struWeekPlanCond.write();
        struWeekPlanCfg.write();

        Pointer lpCond = struWeekPlanCond.getPointer();
        Pointer lpInbuferCfg = struWeekPlanCfg.getPointer();

        if (false == hCNetSDK.NET_DVR_GetDeviceConfig(lUserID, HCNetSDK.NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println("NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50失败" );
            return;
        }
        struWeekPlanCfg.read();
        System.out.println(struWeekPlanCond.dwWeekPlanNumber);

        for(int i=0;i<7;i++)
        {
            for(int j=0;j<8;j++)
            {
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].byEnable = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.bySecond = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.bySecond = 0;
            }
        }

        for(int i=0;i<7;i++)
        {
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].byEnable = 1;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byHour = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byMinute = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.bySecond = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byHour = 4;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byMinute = 59;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.bySecond = 59;

            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[1].byEnable = 1;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[1].struTimeSegment.struBeginTime.byHour = 6;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[1].struTimeSegment.struBeginTime.byMinute = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[1].struTimeSegment.struBeginTime.bySecond = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[1].struTimeSegment.struEndTime.byHour = 12;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[1].struTimeSegment.struEndTime.byMinute = 59;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[1].struTimeSegment.struEndTime.bySecond = 59;

            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[2].byEnable = 1;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[2].struTimeSegment.struBeginTime.byHour = 13;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[2].struTimeSegment.struBeginTime.byMinute = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[2].struTimeSegment.struBeginTime.bySecond = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[2].struTimeSegment.struEndTime.byHour = 23;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[2].struTimeSegment.struEndTime.byMinute = 59;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[2].struTimeSegment.struEndTime.bySecond = 59;
        }
        struWeekPlanCfg.write();

        if (false == hCNetSDK.NET_DVR_SetDeviceConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println( "NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50失败，错误号：" + iErr);
            return;
        }
        System.out.println("NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50成功！");
        hCNetSDK.NET_DVR_Logout(lUserID);
        hCNetSDK.NET_DVR_Cleanup();
        return;
    }

}

